package com.ruoyi.hcmain.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hcmain.domain.HcPrich;
import com.ruoyi.hcmain.domain.HcStuff;
import com.ruoyi.hcmain.mapper.HcPrichMapper;
import com.ruoyi.hcmain.mapper.HcStuffMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hcmain.mapper.HcReceiveMapper;
import com.ruoyi.hcmain.domain.HcReceive;
import com.ruoyi.hcmain.service.IHcReceiveService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收录信息Service业务层处理
 * 
 * @author kylin
 * @date 2023-08-14
 */
@Service
public class HcReceiveServiceImpl implements IHcReceiveService 
{
    @Autowired
    private HcReceiveMapper hcReceiveMapper;
    @Autowired
    private HcPrichMapper hcPrichMapper;
    @Autowired
    private HcStuffMapper hcStuffMapper;
    /**
     * 查询收录信息
     * 
     * @param receiveId 收录信息主键
     * @return 收录信息
     */
    @Override
    public HcReceive selectHcReceiveByReceiveId(Long receiveId)
    {
        return hcReceiveMapper.selectHcReceiveByReceiveId(receiveId);
    }
    @Override
    public List<HcReceive> selectHcReceiveByCreateTime(HcReceive hcReceive)
    {
        return hcReceiveMapper.selectHcReceiveByCreateTime(hcReceive);
    }

    /**
     * 查询收录信息列表
     * 
     * @param hcReceive 收录信息
     * @return 收录信息
     */
    @Override
    public List<HcReceive> selectHcReceiveList(HcReceive hcReceive)
    {
        return hcReceiveMapper.selectHcReceiveList(hcReceive);
    }

    /**
     * 新增收录信息
     * 
     * @param hcReceive 收录信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public int insertHcReceive(HcReceive hcReceive)
    {
        int reslut=0;
        try {

            //总价/数量=单价
            BigDecimal num1=hcReceive.getPriall();
            BigDecimal bignum = null;
            bignum=num1.divide(Convert.toBigDecimal(hcReceive.getCounts()));
            hcReceive.setPrices(bignum);

            HcPrich hcPrich=new HcPrich();
            hcPrich.setStuffId(hcReceive.getStuiffId());
            hcPrich.setPrice(bignum);
            //查询数据内是否有最新数据
            HcPrich lastpri=hcPrichMapper.selectHcPrichByStuffId(hcReceive.getStuiffId());
            float less= 0.00F;
            if(null!=lastpri){
                less=bignum.subtract(lastpri.getPrice()).floatValue();
                hcPrich.setFloat(less);
                hcPrich.setVer(lastpri.getVer()+1);
            }else {
                hcPrich.setVer(1L);

                hcPrich.setFloat(less);
            }
            //物料库增加库存
            HcStuff hcStuff1=hcStuffMapper.selectHcStuffByStuffId(hcReceive.getStuiffId());
            hcStuff1.setCounts(hcStuff1.getCounts()+hcReceive.getCounts());

            hcStuff1.setPrices(bignum);
            hcStuffMapper.updateHcStuff(hcStuff1);
            hcReceive.setCreateTime(DateUtils.getNowDate());
            //增加库存
            hcPrich.setCreateTime(DateUtils.getNowDate());
            int res=hcPrichMapper.insertHcPrich(hcPrich);
            if(res>0){
                hcReceive.setPricesId(hcPrich.getPrichId());
            }else {
                throw new ArithmeticException();
            }
            return hcReceiveMapper.insertHcReceive(hcReceive);
        }catch (Exception e){
           System.out.println(e);
        }

        return reslut;
    }

    /**
     * 修改收录信息
     * 
     * @param hcReceive 收录信息
     * @return 结果
     */
    @Override
    public int updateHcReceive(HcReceive hcReceive)
    {
        return hcReceiveMapper.updateHcReceive(hcReceive);
    }

    /**
     * 批量删除收录信息
     * 
     * @param receiveIds 需要删除的收录信息主键
     * @return 结果
     */
    @Override
    public int deleteHcReceiveByReceiveIds(Long[] receiveIds)
    {
        return hcReceiveMapper.deleteHcReceiveByReceiveIds(receiveIds);
    }

    /**
     * 删除收录信息信息
     * 
     * @param receiveId 收录信息主键
     * @return 结果
     */
    @Override
    public int deleteHcReceiveByReceiveId(Long receiveId)
    {
        return hcReceiveMapper.deleteHcReceiveByReceiveId(receiveId);
    }
}
